package com.jason.bookworm.controller;

import com.jason.bookworm.dao.pojo.Book;
import com.jason.bookworm.dao.pojo.BookClassify;
import com.jason.bookworm.dao.pojo.BorrowRecord;
import com.jason.bookworm.response.Result;
import com.jason.bookworm.service.BookcaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookcaseController {

    @Autowired
    private BookcaseService bookcaseService;

    @GetMapping("/all")
    @Secured("manager")
    public Result<List<Book>> getBooks() {
        return bookcaseService.getAllBook();
    }

    @GetMapping("/{bookID}")
    @Secured("reader")
    public Result<Book> getBook(@PathVariable Integer bookID) {
        return bookcaseService.getBook(bookID);
    }

    //获取指定列表书籍
    @PostMapping("/get")
    @Secured("reader")
    public Result<List<Book>> getBooksByBookIDList(@RequestBody List<String> bookIDList) {
        return bookcaseService.getBooks(bookIDList);
    }

    //按分类和页数获取书籍
    @GetMapping("/get/{num}/{classifyID}/{page}")
    public Result<List<Book>> getBooks(@PathVariable Integer page, @PathVariable Integer classifyID, @PathVariable Integer num) {
        return bookcaseService.getBooks(num, classifyID, page);
    }

    //获取书籍分类列表
    @GetMapping("/classify")
    public Result<List<BookClassify>> getClassifyList() {
        return bookcaseService.getClassifyList();
    }

    //获取图书馆页数
    @GetMapping("/pages/{num}/{classifyID}")
    public Result<Integer> getBookWarePages(@PathVariable Integer classifyID, @PathVariable Integer num) {
        return bookcaseService.getPages(num, classifyID);
    }

    //根据bookID将一本书存入redis
    @PutMapping("/reserve/{bookID}")
    @Secured("reader")
    public Result<Object> reserveOneBook(@PathVariable String bookID) {
        return bookcaseService.reserveOneBook(bookID);
    }

    //根据bookID从redis中删除这本书
    @DeleteMapping("/reserve/{bookID}")
    @Secured("reader")
    public Result<Object> deleteOneBook(@PathVariable String bookID) {
        return bookcaseService.deleteOneBook(bookID);
    }

    //从redis中查询所有书籍
    @GetMapping("/reserve")
    @Secured("reader")
    public Result<List<Book>> getAllReserveBook() {
        return bookcaseService.getAllReserveBook();
    }

    //提交借书记录
    @PostMapping("/reserve")
    @Secured("reader")
    public Result<Object> submitBorrowRecord(@RequestBody List<BorrowRecord> recordList) {
        return bookcaseService.submitBorrowRecord(recordList);
    }

    //获取所有借阅记录
    @GetMapping("/borrow")
    @Secured("reader")
    public Result<List<BorrowRecord>> getAllBorrowRecord() {
        return bookcaseService.getAllBorrowRecord();
    }

    //还书
    @PutMapping("/borrow/{bookID}")
    @Secured("reader")
    public Result<Object> returnOneBook(@PathVariable Integer bookID) {
        return bookcaseService.returnOneBook(bookID);
    }

    //删除已还借阅记录
    @DeleteMapping("/borrow/{recordID}")
    public Result<Object> deleteReturnedRecord(@PathVariable Integer recordID){
        return bookcaseService.deleteReturnedRecord(recordID);
    }
}
